package com.netty.init;

import java.lang.reflect.Method;
import java.util.Map;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.anno.Action;

@Component
public class InitMedia implements ApplicationListener<ContextStartedEvent>,Ordered{

	@Override
	public int getOrder() {
		return 0;
	}

	@Override
	public void onApplicationEvent(ContextStartedEvent event) {
		Map<String, Object> beans = event.getApplicationContext().getBeansWithAnnotation(Controller.class);
		
		for(String key : beans.keySet())
		{
			Object controller = beans.get(key);
			Method[] methods = controller.getClass().getDeclaredMethods();
			for(Method method : methods)
			{
				if(method.isAnnotationPresent(Action.class))
				{
					Action action = method.getAnnotation(Action.class);
					String command = action.value();
					Command.getControllers().put(command, new BeanMethod(controller, method));
				}
			}
		}
	}

}
